package com.geek.ch06.anno;

import com.geek.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    @Select("select * from user where name like concat('%',#{name},'%')")
    public List<User> selectUserByName(String name);

    @Options(useGeneratedKeys = true , keyProperty = "id" , keyColumn = "id")
    @Insert("insert into user values(null , #{name},#{age} , #{gender} ,#{birthday} ,null, #{pwd} , 0)")
    public int insertUser(User user);

    @Update("update user set state=#{state} where name=#{name}")
    public int updateUser(@Param("state") String state , @Param("name") String name);

    @Delete("delete from user where id = #{id}")
    public int deleteUser(int id);

    @Select(
            "<script>" +
                    "select * from user "+
                    "<where>" +
                        "<if test='name!=null and name!=\"\"'>"+
                            "and name like concat('%',#{name},'%') "+
                        "</if>"+
                        "<if test='state!=null and state!=\"\"'>"+
                            "and state = #{state} "+
                        "</if>"+
                    "</where>"+
            "</script>"
    )
    public List<User> selectUsers(@Param("state") String state , @Param("name") String name);

    /**
     * 根据用户ID查询用户信息（供其他Mapper关联查询使用）
     * @param id 用户ID
     * @return 用户信息
     */


    /**
     * 作业3：性别和姓名非空查询，且id in(10,20,30)
     */
    @Select(
            "<script>" +
                    "select * from user " +
                    "<where>" +
                        "<if test='gender != null and gender != \"\"'>" +
                            "and gender = #{gender} " +
                        "</if>" +
                        "<if test='name != null and name != \"\"'>" +
                            "and name = #{name} " +
                        "</if>" +
                        "and id in " +
                        "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
                            "#{id}" +
                        "</foreach>" +
                    "</where>" +
            "</script>"
    )
    public List<User> selectUsersByCondition(@Param("gender") String gender, @Param("name") String name, @Param("ids") List<Integer> ids);
}
